package com.booter.provider;

import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

/**
 * @author xueqiu
 * @date 2023/3/30 14:16
 */
@Slf4j
@Component
public class LocalFileReportProvider implements ReportProvider, ApplicationContextAware {
    private String prefix = "cf:";
    //存储路径 用于测试放到项目根路径下面
    @Value("${report.fileStoreDir}")
    private String fileStoreDir;
    private String root;
    //是否禁用此报表提供者
    private boolean disabled = false;

    @Override
    public InputStream loadReport(String file) {
        if (file.startsWith(prefix)) {
            file = file.substring(prefix.length());
        }
        String fullPath = root + "/" + file;
        try {
            return new FileInputStream(fullPath);
        } catch (FileNotFoundException e) {
            throw new ReportException(e);
        }
    }

    @Override
    public void deleteReport(String file) {
        if (file.startsWith(prefix)) {
            file = file.substring(prefix.length());
        }
        String fullPath = root + "/" + file;
        File f = new File(fullPath);
        if (f.exists()) {
            f.delete();
        }
    }

    @Override
    public List<ReportFile> getReportFiles() {
        File file = new File(root);
        List<ReportFile> list = new ArrayList<>();
        for (File f : Objects.requireNonNull(file.listFiles())) {
            list.add(new ReportFile(f.getName(), new Date(f.lastModified())));
        }
        Collections.sort(list, (f1, f2) -> f2.getUpdateDate().compareTo(f1.getUpdateDate()));
        return list;
    }

    @Override
    public String getName() {
        return "file-provider";
    }

    @Override
    public void saveReport(String file, String content) {
        if (file.startsWith(prefix)) {
            file = file.substring(prefix.length());
        }
        String fullPath = root + "/" + file;
        try (FileOutputStream outStream = new FileOutputStream(fullPath)) {
            IOUtils.write(content, outStream, "utf-8");
        } catch (Exception ex) {
            throw new ReportException(ex);
        }
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setFileStoreDir(String fileStoreDir) {
        this.fileStoreDir = fileStoreDir;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        File file = new File(fileStoreDir);
        log.info("报表目录:{}", file.getAbsolutePath());
        this.root = file.getAbsolutePath();
        File path = new File(file.getAbsolutePath());
        if (!path.exists()) {
            path.mkdirs();
        }
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

//    public static void main(String[] args) throws IOException {
//        File f = new File("doc/report");
//        System.out.println(f.getAbsolutePath());
//        System.out.println(f.getCanonicalPath());
//        System.out.println(f.getPath());
//        LocalFileReportProvider l = new LocalFileReportProvider();
//        System.out.println(l.getClass().getResource("/static/").getPath());
//        System.out.println(l.getClass().getResource("").getPath());
//
//        f = new File("");
//        System.out.println(f.getAbsolutePath());
//        System.out.println(f.getCanonicalPath());
//        System.out.println(f.getPath());
//
//        System.out.println(System.getProperty("user.dir"));
//        System.out.println(System.getProperty("java.class.path"));
//    }
}
